﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using System.Collections;
using Light.Framework.Contract;
using System.ComponentModel;
using Light.Utility;

namespace Light.Framework.Model
{
    /// <summary>
    /// 用户
    /// </summary>
    /// <remarks></remarks>
    [Traceable]
    [ImportField]
    [Serializable]
    [ActiveRecord("Users")]
    [DisplayName("用户")]
    public class User : Entity, IUser
    {
        /// <summary>
        /// OpenID的类型
        /// </summary>
        public enum OpenType
        {
            /// <summary>
            /// QQ登录
            /// </summary>
            QQ,
            /// <summary>
            /// 微信登录
            /// </summary>
            Weixin,
            /// <summary>
            /// 微博登录
            /// </summary>
            Weibo,
        }

        /// <summary>
        /// 登录名
        /// </summary>
        [Property("FName")]
        [DisplayName("登录名")]
        [ImportField("登录名*")]
        public string Name { get; set; }


        [Property]
        [DisplayName("昵称")]
        [ImportField("昵称")]
        public string NickName { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [Property]
        [DisplayName("真实姓名")]
        [ImportField("真实姓名")]
        public string RealName { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        [Property]
        [DisplayName("性别")]
        [ImportField("性别")]
        public string Sex { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [Property]
        [DisplayName("手机号码")]
        [ImportField("手机号码*")]
        public string Phone { get; set; }


        /// <summary>
        /// 是否为系统用户
        /// </summary>
        [Property]
        public bool IsSystem { get; set; }

        /// <summary>
        /// 是否为超级管理员
        /// </summary>
        [Property]
        public bool IsSuper { get; set; }

        /// <summary>
        /// 联系地址
        /// </summary>
        [Property]
        [DisplayName("联系地址")]
        [ImportField("联系地址")]
        public string Address { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [Property]
        [DisplayName("邮箱")]
        [ImportField("邮箱")]
        public string Email { get; set; }

        /// <summary>
        /// 邮箱密码
        /// </summary>
        [Property]
        public string EmailPassword { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Property("FPassword")]
        public string Password { get; set; }

        /// <summary>
        /// 登录次数
        /// </summary>
        [Property]
        [ImportField("登录次数")]
        public int LoginTimes { get; set; }

        /// <summary>
        /// 是否为访客
        /// </summary>
        public bool IsGuest { get; set; }


        [Property]
        public string OpenID_QQ { get; set; }

        [Property]
        public string OpenID_Weixin { get; set; }

        [Property]
        public string OpenID_Weibo { get; set; }


        [Property]
        public string FigureUrl_QQ { get; set; }

        [Property]
        public string FigureUrl_Weixin { get; set; }

        [Property]
        public string FigureUrl_Weibo { get; set; }

        /// <summary>
        /// 最后登录日期
        /// </summary>
        [Property]
        [ImportField("最后登录")]
        public DateTime? TimeLastLogin { get; set; }

        /// <summary>
        /// 安全退出时间
        /// </summary>
        [Property]
        [ImportField("退出登录")]
        public DateTime? TimeLastLogout { get; set; }

        /// <summary>
        /// 用于记录用户登录状态令牌
        /// </summary>
        [Property]
        public string AccessToken { get; set; }

        private IList roles = new List<Role>();
        [HasAndBelongsToMany(typeof(Role),
                Table = "R_User_Role",
                ColumnRef = " Role_ID ",
                ColumnKey = " User_ID ")]
        public IList Roles
        {
            get { return roles; }
            set { roles = value; }
        }

        public bool HasTask(string taskCode)
        {
            foreach (Role role in roles)
            {
                if (role.HasTask(taskCode.Trim()))
                    return true;
            }
            return false;
        }

        public bool HasRole(Role task)
        {
            return roles.Contains(task);
        }

        public string FriendlyName
        {
            get
            {
                return NickName + (!String.IsNullOrEmpty(RoleNames) ? " (" + RoleNames + ") " : "");
            }
        }

        public string RoleNames
        {
            get
            {
                List<string> roleNams = new List<string>();
                foreach (Role role in roles)
                {
                    if (role != null)
                        roleNams.Add(role.Name);
                }
                return string.Join("、", roleNams.ToArray());
            }
        }

        public override string ToString()
        {
            if (!String.IsNullOrEmpty(NickName))
                return NickName;
            else if (!String.IsNullOrEmpty(RealName))
                return RealName;
            else if (!String.IsNullOrEmpty(Name))
                return Name;
            else
                return "用户：" + ID.ToString();
        }

        public bool IsApprole { get; set; }

        public override bool Equals(object obj)
        {
            bool equal = false;
            if (obj != null && obj is User && !this.IsNew)
            {
                equal = ID == ((User)obj).ID;
            }
            else
            {
                equal = base.Equals(obj);
            }
            return equal;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// 用户登录时设置相关信息
        /// </summary>
        public void SetLoginInfos()
        {
            LoginTimes++;
            TimeLastLogin = DateTime.Now;
            AccessToken = WebHelper.CreateAccessToken();
        }

        /// <summary>
        /// 退出登录时设置用户相关信息
        /// </summary>
        public void SetLogoutInfos()
        {
            TimeLastLogout = DateTime.Now;
            AccessToken = null;
        }
    }
}
